package org.jivesoftware.smack.util;

import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils
{
  public static final DateFormat XEP_0082_UTC_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  private static final DateFormat dateFormatter = DateFormatType.XEP_0082_DATE_PROFILE.createFormatter();
  private static final Pattern datePattern = Pattern.compile("^\\d+-\\d+-\\d+$");
  private static final DateFormat timeFormatter = DateFormatType.XEP_0082_TIME_MILLIS_ZONE_PROFILE.createFormatter();
  private static final Pattern timePattern = Pattern.compile("^(\\d+:){2}\\d+.\\d+(Z|([+-](\\d+:\\d+)))$");
  private static final DateFormat timeNoZoneFormatter = DateFormatType.XEP_0082_TIME_MILLIS_PROFILE.createFormatter();
  private static final Pattern timeNoZonePattern = Pattern.compile("^(\\d+:){2}\\d+.\\d+$");
  private static final DateFormat timeNoMillisFormatter = DateFormatType.XEP_0082_TIME_ZONE_PROFILE.createFormatter();
  private static final Pattern timeNoMillisPattern = Pattern.compile("^(\\d+:){2}\\d+(Z|([+-](\\d+:\\d+)))$");
  private static final DateFormat timeNoMillisNoZoneFormatter = DateFormatType.XEP_0082_TIME_PROFILE.createFormatter();
  private static final Pattern timeNoMillisNoZonePattern = Pattern.compile("^(\\d+:){2}\\d+$");
  private static final DateFormat dateTimeFormatter = DateFormatType.XEP_0082_DATETIME_MILLIS_PROFILE.createFormatter();
  private static final Pattern dateTimePattern = Pattern.compile("^\\d+(-\\d+){2}+T(\\d+:){2}\\d+.\\d+(Z|([+-](\\d+:\\d+)))?$");
  private static final DateFormat dateTimeNoMillisFormatter = DateFormatType.XEP_0082_DATETIME_PROFILE.createFormatter();
  private static final Pattern dateTimeNoMillisPattern = Pattern.compile("^\\d+(-\\d+){2}+T(\\d+:){2}\\d+(Z|([+-](\\d+:\\d+)))?$");
  private static final DateFormat xep0091Formatter = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss");
  private static final DateFormat xep0091Date6DigitFormatter = new SimpleDateFormat("yyyyMd'T'HH:mm:ss");
  private static final DateFormat xep0091Date7Digit1MonthFormatter = new SimpleDateFormat("yyyyMdd'T'HH:mm:ss");
  private static final DateFormat xep0091Date7Digit2MonthFormatter = new SimpleDateFormat("yyyyMMd'T'HH:mm:ss");
  private static final Pattern xep0091Pattern = Pattern.compile("^\\d+T\\d+:\\d+:\\d+$");
  private static final List<PatternCouplings> couplings = new ArrayList();
  private static final char[] QUOTE_ENCODE = "&quot;".toCharArray();
  private static final char[] APOS_ENCODE = "&apos;".toCharArray();
  private static final char[] AMP_ENCODE = "&amp;".toCharArray();
  private static final char[] LT_ENCODE = "&lt;".toCharArray();
  private static final char[] GT_ENCODE = "&gt;".toCharArray();
  private static MessageDigest digest = null;
  private static Random randGen = new Random();
  private static char[] numbersAndLetters = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

  static
  {
    TimeZone localTimeZone = TimeZone.getTimeZone("UTC");
    XEP_0082_UTC_FORMAT.setTimeZone(localTimeZone);
    dateFormatter.setTimeZone(localTimeZone);
    timeFormatter.setTimeZone(localTimeZone);
    timeNoZoneFormatter.setTimeZone(localTimeZone);
    timeNoMillisFormatter.setTimeZone(localTimeZone);
    timeNoMillisNoZoneFormatter.setTimeZone(localTimeZone);
    dateTimeFormatter.setTimeZone(localTimeZone);
    dateTimeNoMillisFormatter.setTimeZone(localTimeZone);
    xep0091Formatter.setTimeZone(localTimeZone);
    xep0091Date6DigitFormatter.setTimeZone(localTimeZone);
    xep0091Date7Digit1MonthFormatter.setTimeZone(localTimeZone);
    xep0091Date7Digit1MonthFormatter.setLenient(false);
    xep0091Date7Digit2MonthFormatter.setTimeZone(localTimeZone);
    xep0091Date7Digit2MonthFormatter.setLenient(false);
    couplings.add(new PatternCouplings(datePattern, dateFormatter));
    couplings.add(new PatternCouplings(dateTimePattern, dateTimeFormatter, true));
    couplings.add(new PatternCouplings(dateTimeNoMillisPattern, dateTimeNoMillisFormatter, true));
    couplings.add(new PatternCouplings(timePattern, timeFormatter, true));
    couplings.add(new PatternCouplings(timeNoZonePattern, timeNoZoneFormatter));
    couplings.add(new PatternCouplings(timeNoMillisPattern, timeNoMillisFormatter, true));
    couplings.add(new PatternCouplings(timeNoMillisNoZonePattern, timeNoMillisNoZoneFormatter));
  }

  public static Date parseXEP0082Date(String paramString)
    throws ParseException
  {
    return parseDate(paramString);
  }

  public static Date parseDate(String paramString)
    throws ParseException
  {
    Matcher localMatcher = xep0091Pattern.matcher(paramString);
    if (localMatcher.matches())
    {
      int i = paramString.split("T")[0].length();
      if (i < 8)
      {
        Date localDate = handleDateWithMissingLeadingZeros(paramString, i);
        if (localDate != null)
          return localDate;
      }
      else
      {
        synchronized (xep0091Formatter)
        {
          return xep0091Formatter.parse(paramString);
        }
      }
    }
    else
    {
      ??? = couplings.iterator();
      while (((Iterator)???).hasNext())
      {
        PatternCouplings localPatternCouplings = (PatternCouplings)((Iterator)???).next();
        localMatcher = localPatternCouplings.pattern.matcher(paramString);
        if (localMatcher.matches())
        {
          if (localPatternCouplings.needToConvertTimeZone)
            paramString = localPatternCouplings.convertTime(paramString);
          synchronized (localPatternCouplings.formatter)
          {
            return localPatternCouplings.formatter.parse(paramString);
          }
        }
      }
    }
    synchronized (dateTimeNoMillisFormatter)
    {
      return dateTimeNoMillisFormatter.parse(paramString);
    }
  }

  private static Date handleDateWithMissingLeadingZeros(String paramString, int paramInt)
    throws ParseException
  {
    if (paramInt == 6)
      synchronized (xep0091Date6DigitFormatter)
      {
        return xep0091Date6DigitFormatter.parse(paramString);
      }
    ??? = Calendar.getInstance();
    Calendar localCalendar1 = parseXEP91Date(paramString, xep0091Date7Digit1MonthFormatter);
    Calendar localCalendar2 = parseXEP91Date(paramString, xep0091Date7Digit2MonthFormatter);
    List localList = filterDatesBefore((Calendar)???, new Calendar[] { localCalendar1, localCalendar2 });
    if (!localList.isEmpty())
      return determineNearestDate((Calendar)???, localList).getTime();
    return null;
  }

  private static Calendar parseXEP91Date(String paramString, DateFormat paramDateFormat)
  {
    try
    {
      synchronized (paramDateFormat)
      {
        paramDateFormat.parse(paramString);
        return paramDateFormat.getCalendar();
      }
    }
    catch (ParseException localParseException)
    {
    }
    return null;
  }

  private static List<Calendar> filterDatesBefore(Calendar paramCalendar, Calendar[] paramArrayOfCalendar)
  {
    ArrayList localArrayList = new ArrayList();
    for (Calendar localCalendar : paramArrayOfCalendar)
      if ((localCalendar != null) && (localCalendar.before(paramCalendar)))
        localArrayList.add(localCalendar);
    return localArrayList;
  }

  private static Calendar determineNearestDate(Calendar paramCalendar, List<Calendar> paramList)
  {
    Collections.sort(paramList, new Comparator()
    {
      public int compare(Calendar paramAnonymousCalendar1, Calendar paramAnonymousCalendar2)
      {
        Long localLong1 = new Long(StringUtils.this.getTimeInMillis() - paramAnonymousCalendar1.getTimeInMillis());
        Long localLong2 = new Long(StringUtils.this.getTimeInMillis() - paramAnonymousCalendar2.getTimeInMillis());
        return localLong1.compareTo(localLong2);
      }
    });
    return (Calendar)paramList.get(0);
  }

  public static String formatXEP0082Date(Date paramDate)
  {
    synchronized (dateTimeFormatter)
    {
      return dateTimeFormatter.format(paramDate);
    }
  }

  public static String formatDate(Date paramDate, DateFormatType paramDateFormatType)
  {
    return null;
  }

  public static String parseName(String paramString)
  {
    if (paramString == null)
      return null;
    int i = paramString.lastIndexOf("@");
    if (i <= 0)
      return "";
    return paramString.substring(0, i);
  }

  public static String parseServer(String paramString)
  {
    if (paramString == null)
      return null;
    int i = paramString.lastIndexOf("@");
    if (i + 1 > paramString.length())
      return "";
    int j = paramString.indexOf("/");
    if ((j > 0) && (j > i))
      return paramString.substring(i + 1, j);
    return paramString.substring(i + 1);
  }

  public static String parseResource(String paramString)
  {
    if (paramString == null)
      return null;
    int i = paramString.indexOf("/");
    if ((i + 1 > paramString.length()) || (i < 0))
      return "";
    return paramString.substring(i + 1);
  }

  public static String parseBareAddress(String paramString)
  {
    if (paramString == null)
      return null;
    int i = paramString.indexOf("/");
    if (i < 0)
      return paramString;
    if (i == 0)
      return "";
    return paramString.substring(0, i);
  }

  public static boolean isFullJID(String paramString)
  {
    return (parseName(paramString).length() > 0) && (parseServer(paramString).length() > 0) && (parseResource(paramString).length() > 0);
  }

  public static String escapeNode(String paramString)
  {
    if (paramString == null)
      return null;
    StringBuilder localStringBuilder = new StringBuilder(paramString.length() + 8);
    int i = 0;
    int j = paramString.length();
    while (i < j)
    {
      char c = paramString.charAt(i);
      switch (c)
      {
      case '"':
        localStringBuilder.append("\\22");
        break;
      case '&':
        localStringBuilder.append("\\26");
        break;
      case '\'':
        localStringBuilder.append("\\27");
        break;
      case '/':
        localStringBuilder.append("\\2f");
        break;
      case ':':
        localStringBuilder.append("\\3a");
        break;
      case '<':
        localStringBuilder.append("\\3c");
        break;
      case '>':
        localStringBuilder.append("\\3e");
        break;
      case '@':
        localStringBuilder.append("\\40");
        break;
      case '\\':
        localStringBuilder.append("\\5c");
        break;
      default:
        if (Character.isWhitespace(c))
          localStringBuilder.append("\\20");
        else
          localStringBuilder.append(c);
        break;
      }
      i++;
    }
    return localStringBuilder.toString();
  }

  public static String unescapeNode(String paramString)
  {
    if (paramString == null)
      return null;
    char[] arrayOfChar = paramString.toCharArray();
    StringBuilder localStringBuilder = new StringBuilder(arrayOfChar.length);
    int i = 0;
    int j = arrayOfChar.length;
    label339: 
    while (i < j)
    {
      char c = paramString.charAt(i);
      if ((c == '\\') && (i + 2 < j))
      {
        int k = arrayOfChar[(i + 1)];
        int m = arrayOfChar[(i + 2)];
        if (k == 50)
          switch (m)
          {
          case 48:
            localStringBuilder.append(' ');
            i += 2;
            break;
          case 50:
            localStringBuilder.append('"');
            i += 2;
            break;
          case 54:
            localStringBuilder.append('&');
            i += 2;
            break;
          case 55:
            localStringBuilder.append('\'');
            i += 2;
            break;
          case 102:
            localStringBuilder.append('/');
            i += 2;
            break;
          default:
            break;
          }
        if (k == 51)
          switch (m)
          {
          case 97:
            localStringBuilder.append(':');
            i += 2;
            break;
          case 99:
            localStringBuilder.append('<');
            i += 2;
            break;
          case 101:
            localStringBuilder.append('>');
            i += 2;
            break;
          case 98:
          case 100:
          default:
            break;
          }
        if (k == 52)
        {
          if (m == 48)
          {
            localStringBuilder.append("@");
            i += 2;
            break label339;
          }
        }
        else if ((k == 53) && (m == 99))
        {
          localStringBuilder.append("\\");
          i += 2;
          break label339;
        }
      }
      localStringBuilder.append(c);
      i++;
    }
    return localStringBuilder.toString();
  }

  public static String escapeForXML(String paramString)
  {
    if (paramString == null)
      return null;
    int j = 0;
    int k = 0;
    char[] arrayOfChar = paramString.toCharArray();
    int m = arrayOfChar.length;
    StringBuilder localStringBuilder = new StringBuilder((int)(m * 1.3D));
    while (j < m)
    {
      int i = arrayOfChar[j];
      if (i <= 62)
        if (i == 60)
        {
          if (j > k)
            localStringBuilder.append(arrayOfChar, k, j - k);
          k = j + 1;
          localStringBuilder.append(LT_ENCODE);
        }
        else if (i == 62)
        {
          if (j > k)
            localStringBuilder.append(arrayOfChar, k, j - k);
          k = j + 1;
          localStringBuilder.append(GT_ENCODE);
        }
        else if (i == 38)
        {
          if (j > k)
            localStringBuilder.append(arrayOfChar, k, j - k);
          if ((m <= j + 5) || (arrayOfChar[(j + 1)] != '#') || (!Character.isDigit(arrayOfChar[(j + 2)])) || (!Character.isDigit(arrayOfChar[(j + 3)])) || (!Character.isDigit(arrayOfChar[(j + 4)])) || (arrayOfChar[(j + 5)] != ';'))
          {
            k = j + 1;
            localStringBuilder.append(AMP_ENCODE);
          }
        }
        else if (i == 34)
        {
          if (j > k)
            localStringBuilder.append(arrayOfChar, k, j - k);
          k = j + 1;
          localStringBuilder.append(QUOTE_ENCODE);
        }
        else if (i == 39)
        {
          if (j > k)
            localStringBuilder.append(arrayOfChar, k, j - k);
          k = j + 1;
          localStringBuilder.append(APOS_ENCODE);
        }
      j++;
    }
    if (k == 0)
      return paramString;
    if (j > k)
      localStringBuilder.append(arrayOfChar, k, j - k);
    return localStringBuilder.toString();
  }

  public static synchronized String hash(String paramString)
  {
    if (digest == null)
      try
      {
        digest = MessageDigest.getInstance("SHA-1");
      }
      catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
      {
        System.err.println("Failed to load the SHA-1 MessageDigest. Jive will be unable to function normally.");
      }
    try
    {
      digest.update(paramString.getBytes("UTF-8"));
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      System.err.println(localUnsupportedEncodingException);
    }
    return encodeHex(digest.digest());
  }

  public static String encodeHex(byte[] paramArrayOfByte)
  {
    StringBuilder localStringBuilder = new StringBuilder(paramArrayOfByte.length * 2);
    byte[] arrayOfByte = paramArrayOfByte;
    int k = paramArrayOfByte.length;
    for (int j = 0; j < k; j++)
    {
      int i = arrayOfByte[j];
      if ((i & 0xFF) < 16)
        localStringBuilder.append("0");
      localStringBuilder.append(Integer.toString(i & 0xFF, 16));
    }
    return localStringBuilder.toString();
  }

  public static String encodeBase64(String paramString)
  {
    byte[] arrayOfByte = null;
    try
    {
      arrayOfByte = paramString.getBytes("ISO-8859-1");
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      localUnsupportedEncodingException.printStackTrace();
    }
    return encodeBase64(arrayOfByte);
  }

  public static String encodeBase64(byte[] paramArrayOfByte)
  {
    return encodeBase64(paramArrayOfByte, false);
  }

  public static String encodeBase64(byte[] paramArrayOfByte, boolean paramBoolean)
  {
    return encodeBase64(paramArrayOfByte, 0, paramArrayOfByte.length, paramBoolean);
  }

  public static String encodeBase64(byte[] paramArrayOfByte, int paramInt1, int paramInt2, boolean paramBoolean)
  {
    return Base64.encodeBytes(paramArrayOfByte, paramInt1, paramInt2, paramBoolean ? 0 : 8);
  }

  public static byte[] decodeBase64(String paramString)
  {
    try
    {
      arrayOfByte = paramString.getBytes("UTF-8");
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      arrayOfByte = paramString.getBytes();
    }
    byte[] arrayOfByte = Base64.decode(arrayOfByte, 0, arrayOfByte.length, 0);
    return arrayOfByte;
  }

  public static String randomString(int paramInt)
  {
    if (paramInt < 1)
      return null;
    char[] arrayOfChar = new char[paramInt];
    for (int i = 0; i < arrayOfChar.length; i++)
      arrayOfChar[i] = numbersAndLetters[randGen.nextInt(71)];
    return new String(arrayOfChar);
  }

  private static class PatternCouplings
  {
    Pattern pattern;
    DateFormat formatter;
    boolean needToConvertTimeZone = false;

    public PatternCouplings(Pattern paramPattern, DateFormat paramDateFormat)
    {
      this.pattern = paramPattern;
      this.formatter = paramDateFormat;
    }

    public PatternCouplings(Pattern paramPattern, DateFormat paramDateFormat, boolean paramBoolean)
    {
      this.pattern = paramPattern;
      this.formatter = paramDateFormat;
      this.needToConvertTimeZone = paramBoolean;
    }

    public String convertTime(String paramString)
    {
      if (paramString.charAt(paramString.length() - 1) == 'Z')
        return paramString.replace("Z", "+0000");
      return paramString.replaceAll("([\\+\\-]\\d\\d):(\\d\\d)", "$1$2");
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smack.util.StringUtils
 * JD-Core Version:    0.6.1
 */